package angry.courses.servlets;

import java.util.List;

import javax.naming.InitialContext;

import angry.courses.beans.View;
import angry.courses.beans.ViewBean;
import angry.courses.servlets.CourseDelegate;
import angry.courses.servlets.HtmlFormatter;
import angry.courses.entities.CourseEntity;
import angry.courses.entities.StudentEntity;

public class ViewDelegate {
	private View viewBean;
  
	public ViewDelegate() {
		try {
			InitialContext ctx = new InitialContext();
			viewBean = (View) ctx.lookup("angry.courses.beans.ViewBean");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
	public List<String> getStudentsForCourse(String name) {
		return viewBean.getStudentsForCourse(name);
	}
	
	public boolean addCourseToView(StringBuffer buf, HtmlFormatter formater, String name) {
    	CourseDelegate courseDelegate = new CourseDelegate();
    	CourseEntity course = courseDelegate.getCourse(name);
    	
    	if (course != null) {
	    	List<String> studentsList = getStudentsForCourse(name);
	    	buf.append("<b>");
	   	    buf.append(course.getName() + "<br>");
	   	    buf.append("Year of study: " + course.getYear() + "<br>");
	   	 	buf.append("Professor name: " + course.getProfessor() + "<br>");
	   	    buf.append("</b>");
	   	    buf.append("<table border=\"1\">");
	   	    for (String student : studentsList) {
	   	    	buf.append(formater.tr(300, student));
	   	    }
	   	    buf.append("</table>");
	   	    buf.append("<br><br>");	
	   	    return true;
    	} 
    	return false;
    }
	
	public List<String> getCoursesForStudent(String name, int semester) {
		return viewBean.getCoursesForStudent(name, semester);
	}

    public boolean addStudentToView(StringBuffer buf, HtmlFormatter formater, String name) {
    	StudentDelegate studentDelegate = new StudentDelegate();
    	StudentEntity student = studentDelegate.getStudentByName(name);    	
    	
    	if (student != null) {
	    	List<String> coursesList1 = getCoursesForStudent(name, 1);
	    	List<String> coursesList2 = getCoursesForStudent(name, 2);
	   	    completeLists(coursesList1, coursesList2);
	   	    buf.append("<b>");
	   	    buf.append(student.getName() + "<br>");
	   	    buf.append("Year of study: " + student.getYearOfStudy() + "<br>");
	   	    buf.append("</b>");
	   	    buf.append("<table border=\"1\">");
	   	    buf.append(formater.th("Semester 1") + formater.th("Semester2"));
	   	    for (int i = 0; i < coursesList1.size(); i++) {
	   	    	buf.append(formater.tr(300, coursesList1.get(i), coursesList2.get(i)));
	   	    }
	   	    buf.append("</table>");
	   	    buf.append("<br><br>");
	   	    // student was added
	   	    return true;
    	}
    	// student not added
    	return false;
    }

	private void completeLists(List<String> coursesList1,
			List<String> coursesList2) {
		int size1 = coursesList1.size();
		int size2 = coursesList2.size();
		if (size1 < size2) {
			for (int i = 0; i < size2 - size1; i++) {
				coursesList1.add("");
			}
		} else {
			for (int i = 0; i < size1 - size2; i++) {
				coursesList2.add("");
			}
		}
	}
	
	public void addCoursesForYear(StringBuffer buf, HtmlFormatter formater, int year) {
    	buf.append(formater.heading(3, "Year of study: " + year));
	    for (int i = 1; i <= 2; i++) {
    		addCoursesForSemester(buf, formater, year, i);
	    }
    }
    
    private void addCoursesForSemester(StringBuffer buf, HtmlFormatter formater, int year, int semester) {
    	CourseDelegate courseDelegate = new CourseDelegate();
    	List<CourseEntity> courses = courseDelegate.getCourses(year, semester);
    	if (courses != null) {
	    	buf.append(formater.heading(4, "Semester " + semester));
	   	    buf.append("<table border=\"1\">");
	   	    buf.append(formater.th("Course name") + formater.th("Professor name"));
	   	    for (CourseEntity course : courses) {
	   	    	buf.append(formater.tr(300, course.getName(), course.getProfessor()));
	   	    }
	   	    buf.append("</table>");
	   	    buf.append("<br><br>");	   	    
    	}
    }
}
